<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * PC读取数据的模型
 * @author moon 2018.11.01
 */
class General extends CI_Model{
    /**
     * 构造函数
     * @param null
     * @return object
     * @author moon 2018.12.18
     */
    public function __construct()
    {
        parent::__construct();
    }
	
    /**
     * 获取新闻列表
     * @param $params 参数
     * @return array
     * @author moon 2018.12.19
     */
    public function getNews($params)
	{
        $resArr['list'] = array();
        $resArr['counts'] = 0;
       	$start = $params['start'];
		$num =  $params['num'];
			
		if(isset($params['cat_id'])){
			//获取总数
			$p_counts = $this->getNewsCounts($params);
			$this->db->where("cat_id",$params['cat_id']);
			$this->db->select("news_id,news_title,news_thumb,news_brief,create_time");
			$this->db->order_by('create_time', 'DESC');
			$this->db->limit($num,$start);
			$this->db->from('news');
			$query = $this->db->get();
			$resArr['list'] = $query->result_array();
			$resArr['counts'] = $p_counts;					
		}		
		

        return $resArr;
		
    }

    /**
     * 获取新闻总数
     * @param $params 参数数组
	   'kw'=0单个目录ID
     * @return array
     * @author moon 2018.12.19
     */
    public function getNewsCounts($params)
	{
		$p_counts = 0;
		if(isset($params['cat_id'])){
			$this->db->where("cat_id",$params['cat_id']);
		    $this->db->from('news');
			$query = $this->db->get();
			$p_counts = $query->num_rows();
		}
		return $p_counts;
    }


    /**
     * 获取关于麦森信息
     * @return array
     * @author moon 2018.12.19
     */
    public function getAboutNews()
	{
        $this->db->select("news_id,news_title,news_thumb,content");
        $this->db->where("cat_id",1);
        $this->db->order_by('create_time', 'ASC');
        $this->db->limit(5,0);
        $this->db->from('news');
        $query = $this->db->get();
        return $query->result_array();
    }

    /**
     * 获取行业解决方案信息
     * @return array
     * @author moon 2018.12.19
     */
    public function getSolutionNews()
	{
        $this->db->select("news_id,news_title,news_thumb,news_brief");
        $this->db->where("cat_id",11);
        $this->db->order_by('create_time', 'ASC');
        $this->db->limit(24,0);
        $this->db->from('news');
        $query = $this->db->get();
        return $query->result_array();
    }


    /**
     * 获取新闻信息详情
     * @return array
     * @author moon 2018.12.19
     */
    public function getNewsDetail($news_id)
	{
        $this->db->select("news_id,news_title,news_thumb,content,create_time");
        $this->db->where("news_id",$news_id);
		$this->db->where("is_release",1);
        $this->db->from('news');
        $query = $this->db->get();
        if($query->num_rows() < 1)
            return false;
        return $query->row_array();
    }

    /**
     * 获取产品列表
     * @param $params 参数数组
	   'cat_id'=0单个目录ID
	   'cat_ids'=多个目录ID
	   'brand_id'=>0单个品牌ID
	   'brand_ids'=>0多个品牌ID
     * @return array
     * @author moon 2018.12.19
     */
    public function getAllGoodsList($params)
	{
		$sql = "SELECT brand_id,brand_name,cat_id,cat_name,attr_id,attr_name 
				FROM zft_goods 
				WHERE `is_delete` = '0'  and `is_on_sale` = '1' ";
		if(isset($params['kw'])){
			//根据产品sku,产品名称，产品型号，产品关键字
			$kw = trim($params['kw']);
			$sql .= "AND (`goods_sn` like '%$kw%' OR  `goods_name` LIKE '%$kw%' ESCAPE '!'
					OR  `product_keys` LIKE '%$kw%' ESCAPE '!'
					OR  `brand_name` LIKE '%$kw%' ESCAPE '!'
					OR  `attr_name` LIKE '%$kw%' ESCAPE '!'
					OR `product_model` like '%$kw%') ";
		}		
		$sql .= "ORDER BY add_time DESC ";
        return  $this->db->query($sql)->result_array();
    }

    /**
     * 获取产品品牌和属性
     * @param $params 参数数组
	   'cat_id'=0单个目录ID
	   'cat_ids'=多个目录ID
	   'brand_id'=>0单个品牌ID
	   'brand_ids'=>0多个品牌ID
     * @return array
     * @author moon 2018.12.19
     */
    public function getBrandsAttrsByCatid($params)
	{
        $this->db->select("brand_id,brand_name,attr_id,attr_name");
		$this->db->where("is_delete",0);
		$this->db->where("is_on_sale",1);
		if(isset($params['cat_ids'])){
			$this->db->where_in("cat_id",$params['cat_ids']);
		}
        $this->db->from('goods');
        $query = $this->db->get();
        return $query->result_array();
    }

    /**
     * 获取商城商品列表
     * @param $is_new 是否新品推荐
     * @param $is_promote 是否限时特惠
     * @return array
     * @author moon 2018.12.19
     */
    public function getMallGoodsList($keys,$value)
	{
        $this->db->select("goods_id,goods_sn,brand_name,goods_name,goods_thumb,product_model,add_time");
       	$start = 0;
		$num = 8;
		$res = array();
		if(isset($keys)){
			$this->db->where($keys,$value);
			$this->db->where("is_delete",0);
			$this->db->where("is_on_sale",1);
			$this->db->order_by('add_time', 'DESC');
			$this->db->limit($num,$start);
			$this->db->from('goods');
			$query = $this->db->get();
			$res = $query->result_array();		
		}
        return $res;
    }
	
	
    /**
     * 获取相关推荐
     * @param $cat_id 同目录下的产品
     * @return array
     * @author moon 2018.12.19
     */
    public function getRecommendGoodsList($params)
	{
        $this->db->select("goods_id,goods_sn,goods_name,goods_thumb,add_time");
       	$start = 0;
		$num = 10;
		$this->db->where("is_delete",0);
		$this->db->where("is_on_sale",1);
		$this->db->where_not_in("goods_id",$params['goods_id']);
		if(isset($params['cat_id'])){
			$this->db->where("cat_id",$params['cat_id']);
		}
        $this->db->order_by('add_time', 'DESC');
        $this->db->limit($num,$start);
        $this->db->from('goods');
        $query = $this->db->get();
        return $query->result_array();
    }

    /**
     * 获取产品列表
     * @param $start 开始偏移量
     * @param $params 参数数组
	   'start'=0开始偏移量
	   'num'=10获取的条数
	   'kw'=0单个目录ID
     * @return array
     * @author moon 2018.12.19
     */
    public function getGoodsList($params)
	{
        $resArr['list'] = array();
        $resArr['counts'] = 0;
		//获取总数
		$p_counts = $this->getGoodsCounts($params);
		$sql = "SELECT goods_id,cat_id,attr_id,goods_sn,goods_name,brand_id,brand_name,market_price,shop_price,goods_thumb,
		product_model,min_num,shipment_date,show_price,product_unit,product_keys,add_time 
		FROM zft_goods 
		WHERE `is_delete` = '0'  and `is_on_sale` = '1' ";
	   	$start = $params['start'];
		$num =  $params['num'];
		
		if(isset($params['brand_id'])){
			$sql .= "AND `brand_id` = '".$params['brand_id']."' ";
		}
		if(isset($params['attr_id'])){
			$sql .= "AND `attr_id` = '".$params['attr_id']."' ";
		}
		if(isset($params['cat_ids']) && $params['cat_ids']<>""){
			$sql .= "AND `cat_id` in (".$params['cat_ids'].") ";
		}		
		
		$sql .= "ORDER BY add_time DESC LIMIT $start,$num ";
        $resArr['list'] = $this->db->query($sql)->result_array();
        $resArr['counts'] = $p_counts;
		return $resArr;
    }


    /**
     * 获取搜索产品列表
     * @param $start 开始偏移量
     * @param $params 参数数组
	   'start'=0开始偏移量
	   'num'=10获取的条数
	   'kw'=0单个目录ID
     * @return array
     * @author moon 2018.12.19
     */
    public function getSearchGoodsList($params)
	{
        $resArr['list'] = array();
        $resArr['counts'] = 0;
		//获取总数
		$p_counts = $this->getGoodsCounts($params);
		$sql = "SELECT goods_id,cat_id,attr_id,goods_sn,goods_name,brand_id,brand_name,market_price,shop_price,goods_thumb,
		product_model,min_num,shipment_date,show_price,product_unit,product_keys,add_time 
		FROM zft_goods 
		WHERE `is_delete` = '0' and `is_on_sale` = '1' ";
	   	$start = $params['start'];
		$num =  $params['num'];
		
		if(isset($params['kw'])){
			//根据产品sku,产品名称，产品型号，产品关键字

			$kw = trim($params['kw']);

			$sql .= "AND (`goods_sn` LIKE '%$kw%' OR  `goods_name` LIKE '%$kw%' ESCAPE '!'
					OR  `product_keys` LIKE '%$kw%' ESCAPE '!'
					OR  `brand_name` LIKE '%$kw%' ESCAPE '!'
					OR  `attr_name` LIKE '%$kw%' ESCAPE '!'
					OR `product_model` LIKE '%$kw%') ";
		}
		
		if(isset($params['brand_id'])){
			$sql .= "AND `brand_id` = '".$params['brand_id']."' ";
		}
		if(isset($params['attr_id'])){
			$sql .= "AND `attr_id` = '".$params['attr_id']."' ";
		}
		if(isset($params['cat_id'])){
			$sql .= "AND `cat_id` = '".$params['cat_id']."' ";
		}		
		
		$sql .= "ORDER BY add_time DESC LIMIT $start,$num ";
        $resArr['list'] = $this->db->query($sql)->result_array();
        $resArr['counts'] = $p_counts;
		return $resArr;
    }

    /**
     * 获取产品总数
     * @param $params 参数数组
	   'kw'=0单个目录ID
     * @return array
     * @author moon 2018.12.19
     */
    public function getGoodsCounts($params)
	{
		$sql = "SELECT goods_id	FROM zft_goods WHERE `is_delete` = '0'  and `is_on_sale` = '1' ";
		if(isset($params['kw'])){
			//根据产品sku,产品名称，产品型号，产品关键字
			$kw = trim($params['kw']);
			$sql .= "AND (`goods_sn` LIKE '%$kw%' OR  `goods_name` LIKE '%$kw%' ESCAPE '!'
					OR  `product_keys` LIKE '%$kw%' ESCAPE '!'
					OR  `brand_name` LIKE '%$kw%' ESCAPE '!'
					OR  `attr_name` LIKE '%$kw%' ESCAPE '!'
					OR `product_model` LIKE '%$kw%') ";
		
		}
		if(isset($params['brand_id'])){
			$sql .= "AND `brand_id` = '".$params['brand_id']."' ";
		}
		if(isset($params['attr_id'])){
			$sql .= "AND `attr_id` = '".$params['attr_id']."' ";
		}
		if(isset($params['cat_id'])){
			$sql .= "AND `cat_id` = '".$params['cat_id']."' ";
		}
		if(isset($params['cat_ids']) && $params['cat_ids']<>""){
			$sql .= "AND `cat_id` in (".$params['cat_ids'].") ";
		}		
				
        return $this->db->query($sql)->num_rows();
    }
	
	/*
	
		获取当前URL地址
	*/
	public function get_url()
	{
		$sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
		$php_self = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
		$path_info = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
		$relate_url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $php_self.(isset($_SERVER['QUERY_STRING']) ? '?'.$_SERVER['QUERY_STRING'] : $path_info);
		return $sys_protocal.(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '').$relate_url;
	}	
	
	
    /**
     * 根据二级目录获取上级目录
     * @param arr_catid 二级目录ID数组
     * @return array: array('一级ID'=>'一级名称','child'=>array()
     * @author moon 2018.12.19
     */
    public function getSearchCatList($arr_catid)
	{
        
		//根据arr_catid查找父级目录信息
		
		//查找父级ID
        $this->db->select("cat_id,parent_id,cat_name");
		if(empty($arr_catid)){
			return false;
		}else{
			$cat_ids = implode(",",$arr_catid);
			//去除末尾的,号
			$cat_ids = rtrim($cat_ids, ",");
			$this->db->where_in("cat_id",$cat_ids);
			$this->db->where("is_show",1);
			$this->db->order_by('sort_order', 'ASC');
			$this->db->limit(20,0);
			$this->db->from('news');
			
			$query = $this->db->get();
			$child_arr = $query->result_array();
			//根据child_id查找父目录信息
			for($i=0;$i<count($child_arr);$i++){
			
			
			
			}
			
			
		}
    }	
	
    /**
     * 获取产品详细内容
     * @param $goods_sn 产品sku号
     * @return array
     * @author moon 2018.12.19
     */
	public function getProductsDetailBySku($goods_sn)
	{
	
        $this->db->select("*");
        $this->db->where("goods_sn",$goods_sn);
        $this->db->where("is_on_sale",1);
        $this->db->where("is_delete",0);
        $this->db->from('goods');
        $query = $this->db->get();
        if($query->num_rows() < 1)
            return false;
        return $query->row_array();
	
	}
	
    /**
     * 获取产品相册
     * @param $goods_sn 产品sku号
     * @return array
     * @author moon 2018.12.19
     */
	public function getGoodsGalleryByid($goods_id)
	{
	    if(!$goods_id){
			return false;
		}
		$this->db->select("img_id,goods_id,thumb_url,img_original");
        $this->db->where("goods_id",$goods_id);		
        $this->db->order_by('img_id', 'ASC');
        $this->db->limit(5);
        $this->db->from('goods_gallery');
        $query = $this->db->get();
        return $query->result_array();	
	}	
	
	
	/**
	 * 取得当前位置
	 *
	 * @access  public
	 * @param   integer     $cat_id    分类ID（只有商品及分类、文章及分类用到）
	 * @param   integer     $type   1.商品分类 2.文章分类
	 * @return  str
	 */
	function assign_ur_here($cat_id = 0, $type=1)
	{
		$ur_here    = '<a href="/">首页</a>';
		$url = "";
		if($type==1){
			//商品分类
			$url = "/category.html";
			$cat_arr = $this->get_goodsParent_cats($cat_id);
		}else{
			//文章分类
			$url = "/newscat/";			
			$cat_arr = $this->get_newsParent_cats($cat_id);
		}
		/* 循环分类 */
		if (!empty($cat_arr))
		{
			krsort($cat_arr);
			foreach ($cat_arr AS $val)
			{
				$ur_here   .= ' <code>&gt;</code> <a href="' . $url.'?catid='.$val['cat_id'] . '">' .
								htmlspecialchars($val['cat_name']) . '</a>';
			}
		}
		return $ur_here;
	}
	
	/**
	 * 去除html标签
	 *
	 * @access  public
	 * @param   string $txt html字符串
	 * @return  array
	 */	
	function html2text($txt)
	{
		$txt = strip_tags($txt);
		$txt = str_replace("首页 &gt; ","",$txt);
		$txt = str_replace(" &gt; ","/",$txt);
		return $txt;
	}

	/**
	 * 获得指定商品分类的所有上级分类
	 *
	 * @access  public
	 * @param   integer $cat_id    分类编号
	 * @return  array
	 */
	function get_goodsParent_cats($ncat_id)
	{
		if ($ncat_id == 0)
		{
			return array();
		}
		$this->db->select("cat_id, cat_name, parent_id");
        $this->db->where("is_show",1);		
        $this->db->order_by('cat_id', 'ASC');
        $this->db->from('category');
        $query = $this->db->get();	
		$arr = $query->result_array();

		if (empty($arr))
		{
			return array();
		}
	
		$index = 0;
		$cats  = array();
		while (1)
		{
			foreach ($arr AS $row)
			{
				if ($ncat_id == $row['cat_id']){
					
					$ncat_id = $row['parent_id'];
	
					$cats[$index]['cat_id']   = $row['cat_id'];
					$cats[$index]['cat_name'] = $row['cat_name'];
	
					$index++;
					break;
				}
			}
	
			if ($index == 0 || $ncat_id == 0)
			{
				break;
			}
		}
	
		return $cats;
	}
	
	/**
	 * 获得指定新闻分类的所有上级分类
	 *
	 * @access  public
	 * @param   integer $cat_id    分类编号
	 * @return  array
	 */
	function get_newsParent_cats($ncat_id)
	{
		if ($ncat_id == 0)
		{
			return array();
		}
		$this->db->select("cat_id, cat_name, parent_id");
        $this->db->where("is_show",1);		
        $this->db->order_by('cat_id', 'ASC');
        $this->db->from('article_cat');
        $query = $this->db->get();	
		$arr = $query->result_array();

		if (empty($arr))
		{
			return array();
		}
	
		$index = 0;
		$cats  = array();
		while (1)
		{
			foreach ($arr AS $row)
			{
				if ($ncat_id == $row['cat_id']){
					
					$ncat_id = $row['parent_id'];
	
					$cats[$index]['cat_id']   = $row['cat_id'];
					$cats[$index]['cat_name'] = $row['cat_name'];
	
					$index++;
					break;
				}
			}
	
			if ($index == 0 || $ncat_id == 0)
			{
				break;
			}
		}
	
		return $cats;
	}

	/**
	 * 获得所有一级分类
	 *
	 * @access  public
	 * @return  array
	 */
	function get_parent_cat()
	{
		$catArr = array();
        $this->db->where("is_show",1);
        $this->db->where("parent_id",0);
		$this->db->order_by('sort_order', 'ASC');
        $this->db->from('category');
        $query = $this->db->get();
       	$res = $query->result_array();
		foreach ($res AS $row){
			$catArr[$row['cat_id']] = $row['cat_name'];
		}
		return $catArr;
	}
	
	/**
	 * 获得指定分类的所有子分类
	 *
	 * @access  public
	 * @param   integer $cat_id    分类编号
	             $t 返回的样式 0数组形式,1字符形式
	 * @return  array
	 */
	function get_goodsChild_tree($tree_id = 0)
	{
		$three_arr = array();
		$returns = array();
        $this->db->where("is_show",1);
        $this->db->where("parent_id",$tree_id);
        $this->db->from('category');
        $query = $this->db->get();
        $counts = $query->num_rows();
		$in_cats = "";
		if ($counts || $tree_id == 0)
		{
	        $this->db->select("cat_id,cat_name,parent_id,is_show");
			$this->db->where("parent_id",$tree_id);
			$this->db->where("is_show",1);
			$this->db->order_by('sort_order', 'ASC');
			$this->db->from('category');
			$query = $this->db->get();
			$res = $query->result_array();
		
			foreach ($res AS $row)
			{
				if ($row['is_show'])
	
				   $three_arr[$row['cat_id']]['id']   = $row['cat_id'];
				   $three_arr[$row['cat_id']]['name'] = $row['cat_name'];
	
				   if (isset($row['cat_id']) != NULL)
					   {
						   $three_arr[$row['cat_id']]['childcat_id'] = $this->get_goodsChild_tree($row['cat_id']);
						   $in_cats .= $row['cat_id'].",";
				}
			}
		}
		
		if($in_cats<>""){
			$in_cats = rtrim($in_cats,',');
		}
		
		$returns['three_arr'] = $three_arr;
		$returns['in_cats'] = $in_cats;
		return $returns;

	}


    /**
     * 获取广告位所在的广告
     * @param null
     * @return array
     * @author moon 2017.11.01
     */
    public function getAd($position_id){
        $this->db->select("ad_name,ad_link,ad_code");
        $this->db->where("position_id",$position_id);
        $this->db->where("enabled",1);
        $this->db->order_by('show_order', 'ASC');
        $this->db->limit(5);
        $this->db->from('ad');
        $query = $this->db->get();
        return $query->result_array();
    }

    /**
     * 获取推荐品牌
     * @param null
     * @return array
     * @author moon 2017.11.01
     */
    public function getRecommendBrandList(){
        $this->db->select("brand_id,brand_name,brand_logo,brand_desc,site_url");
        $this->db->where("is_show",1);
        $this->db->where("recommend",1);
        $this->db->order_by('sort_order', 'ASC');
        $this->db->limit(4);
        $this->db->from('brand');
        $query = $this->db->get();
        return $query->result_array();
    }	
	
	public function isMobile() {
		//如果有HTTP_X_WAP_PROFILE则一定是移动设备
		if (isset ($_SERVER['HTTP_X_WAP_PROFILE'])){
			return true;
		}
		//如via信息有wap一定是移动设备
		//部分服务商会屏蔽该信息
		if (isset ($_SERVER['HTTP_VIA'])){
			//找不到为flase,否则为true
			return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
		}
		//判断手机发送的客户端标志,兼容性有待提高
		if (isset ($_SERVER['HTTP_USER_AGENT'])) {

			$clientkeywords = array (
								'nokia',
								'sony',
								'ericsson',
								'mot',
								'samsung',
								'htc',
								'sgh',
								'lg',
								'sharp',
								'sie-',
								'philips',
								'panasonic',
								'alcatel',
								'lenovo',
								'iphone',
								'ipod',
								'blackberry',
								'meizu',
								'android',
								'netfront',
								'symbian',
								'ucweb',
								'windowsce',
								'palm',
								'operamini',
								'operamobi',
								'openwave',
								'nexusone',
								'cldc',
								'midp',
								'wap',
								'mobile'
								);
		
		//从HTTP_USER_AGENT中查找手机浏览器的关键字
		if(preg_match("/(".implode('|',$clientkeywords).")/i", 
		strtolower($_SERVER['HTTP_USER_AGENT']))) {
					return true;
				}
			}
		
		//协议法，因为有可能不准确，放到最后判断
		if (isset ($_SERVER['HTTP_ACCEPT'])) {
		//如果只支持wml并且不支持html那一定是app
		//如果支持wml和html但是wml在html之前则是app
			if((strpos($_SERVER['HTTP_ACCEPT'],'vnd.wap.wml')!==false)&&(strpos($_SERVER['HTTP_ACCEPT'],'text/html') === false|| (strpos($_SERVER['HTTP_ACCEPT'],'vnd.wap.wml') <strpos($_SERVER['HTTP_ACCEPT'],'text/html')))){
				   return true;
			}
		}
 	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

   
}